use rusqlite::{Connection, Result};

fn count_records(db_path: &str) -> Result<()> {
    println!("\n查询 {} 数据库记录数：", db_path);
    println!("{:=<40}", "");
    
    // 连接数据库
    let conn = Connection::open(db_path)?;
    
    // 查询notice_table表的记录数
    let notice_count: usize = conn.query_row(
        "SELECT COUNT(*) FROM notice_table",
        [],
        |row| row.get(0)
    )?;
    println!("notice_table: {:6} 条记录", notice_count);
    
    // 查询target_table表的记录数
    let target_count: usize = conn.query_row(
        "SELECT COUNT(*) FROM target_table",
        [],
        |row| row.get(0)
    )?;
    println!("target_table: {:6} 条记录", target_count);
    
    // 查询goods_table表的记录数
    let goods_count: usize = conn.query_row(
        "SELECT COUNT(*) FROM goods_table",
        [],
        |row| row.get(0)
    )?;
    println!("goods_table:  {:6} 条记录", goods_count);
    
    // 计算总记录数
    let total = notice_count + target_count + goods_count;
    println!("{:=<40}", "");
    println!("总记录数:     {:6} 条", total);
    
    Ok(())
}

fn main() {
    println!("数据库记录数量查询程序");
    println!("{:=<40}", "");
    
    // 查询jntujy.db
    if let Err(e) = count_records("jntujy.db") {
        println!("查询jntujy.db失败: {}", e);
    }
    
    // 查询jntujy11.db
    if let Err(e) = count_records("jntujy11.db") {
        println!("查询jntujy11.db失败: {}", e);
    }
}